home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 11 Learning / 09 Laramée / Entities.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-08-22  |  4.3 KB  |  192 lines

  1. /****************************************************************
  2.  * Entity Class implementation
  3.  ***************************************************************/
  4.  
  5. #include "Globals.h"
  6. #include "Entities.h"
  7. #include "Troll.h"
  8. #include <stdlib.h>
  9. #include <iostream.h>
  10.  
  11.  
  12. /***************************************************************
  13.  * ENTITY Implementation
  14.  ***************************************************************/
  15.  
  16. Troll * Entity::ptrTroll = 0;
  17. WorldGrid * Entity::ptrGrid = 0;
  18.  
  19.  
  20. // The grid wraps around; if movement goes overboard, the entity
  21. // magically warps to the other end of the world
  22. void Entity::MoveEntity( int dx, int dy )
  23. {
  24.     int newX = GetX() + dx;
  25.     if ( newX < 0 )
  26.         newX += WORLD_GRID_SIZE;
  27.     if ( newX >= WORLD_GRID_SIZE )
  28.         newX -= WORLD_GRID_SIZE;
  29.     int newY = GetY() + dy;
  30.     if ( newY < 0 )
  31.         newY += WORLD_GRID_SIZE;
  32.     if ( newY >= WORLD_GRID_SIZE )
  33.         newY -= WORLD_GRID_SIZE;
  34.  
  35.     if ( ExportedClass() == ENTITY_TROLL )
  36.     {
  37.         ptrGrid->MoveTroll( newX, newY );
  38.     }
  39.     else
  40.     {
  41.         ptrGrid->MoveEntity( GetWorldID(), newX, newY );
  42.     }
  43.  
  44.     SetX( newX );
  45.     SetY( newY );
  46. }
  47.  
  48.  
  49. /******************************************************************
  50.  * SHEEP Implementation
  51.  *****************************************************************/
  52.  
  53. bool Sheep::Update()
  54. {
  55.     if( IsDead )
  56.         return true;
  57.  
  58.     // If the troll is in the sheep's square, the sheep is meat
  59.     if( ptrGrid->DistanceFromTroll( GetWorldID() ) == 0 )
  60.     {
  61.         IsDead = true;
  62.         ptrGrid->UnRegister( GetWorldID() );
  63.     }
  64.  
  65.     // Two cases: either there is a troll nearby, in which
  66.     // case the sheep runs away, or there isn't, in which
  67.     // case the sheep wanders aimlessly
  68.     if( ptrGrid->IsTrollWithinRange( GetWorldID() ) )
  69.     {
  70.         int dx, dy;
  71.         if ( ptrGrid->GetTrollX() < GetX() )
  72.             dx = 1;
  73.         else
  74.             dx = -1;
  75.         if ( ptrGrid->GetTrollY() < GetY() )
  76.             dy = 1;
  77.         else
  78.             dy = -1;
  79.         MoveEntity( dx, dy );
  80.     }
  81.     else
  82.     {
  83.         int dx = rand() % 3 - 1;
  84.         int dy = rand() % 3 - 1;
  85.         MoveEntity( dx, dy );
  86.     }
  87.         
  88.     return true;
  89. }
  90.  
  91.  
  92. /***************************************************************
  93.  * KNIGHT Implementation
  94.  ***************************************************************/
  95.  
  96. bool Knight::Update()
  97. {
  98.     // Dead knights don't do much of anything
  99.     if ( IsDead )
  100.         return true;
  101.  
  102.     // Should the knight go berserk?
  103.     if ( !IsBerserk && ptrGrid->IsTrollWithinRange( GetWorldID() ) )
  104.     {
  105.         IsBerserk = true;
  106.     }
  107.  
  108.     // If the troll is in a safe haven, the knight automatically
  109.     // loses track
  110.     if ( ptrGrid->HowManyCloseToTroll( ENTITY_HAVEN, 0 ) > 0 )
  111.     {
  112.         IsBerserk = false;
  113.     }
  114.  
  115.     // If the knight is berserk, he pursues the troll; otherwise,
  116.     // he wanders about aimlessly
  117.     if ( IsBerserk )
  118.     {
  119.         int dx, dy;
  120.         if ( ptrGrid->GetTrollX() < GetX() )
  121.             dx = -1;
  122.         else if ( ptrGrid->GetTrollX() > GetX() )
  123.             dx = 1;
  124.         else
  125.             dx = 0;
  126.         if ( ptrGrid->GetTrollY() < GetY() )
  127.             dy = -1;
  128.         else if ( ptrGrid->GetTrollY() > GetY() )
  129.             dy = 1;
  130.         else
  131.             dy = 0;
  132.         MoveEntity( dx, dy );
  133.     }
  134.     else
  135.     {
  136.         int dx = rand() % 3 - 1;
  137.         int dy = rand() % 3 - 1;
  138.         MoveEntity( dx, dy );
  139.     }
  140.  
  141.     if ( ptrGrid->GetTrollX() == GetX() && ptrGrid->GetTrollY() == GetY() )
  142.     {
  143.         IsDead = ptrTroll->SendFightMessage();
  144.         if( IsDead )
  145.             ptrGrid->UnRegister( GetWorldID() );
  146.     }
  147.  
  148.     return true;
  149. }
  150.  
  151.  
  152. /***************************************************************
  153.  * TOWER Implementation
  154.  **************************************************************/
  155.  
  156. bool Tower::Update()
  157. {
  158.     // If there is a troll within firing range, shoot at it!
  159.     if ( ptrGrid->IsTrollWithinRange( GetWorldID() ) )
  160.     {
  161.         ptrTroll->SendDamageMessage( rand() % 3 );
  162.     }
  163.  
  164.     return true;
  165. }
  166.  
  167.  
  168.  
  169. /***************************************************************
  170.  * TRAP Implementation
  171.  ***************************************************************/
  172.  
  173. bool Trap::Update()
  174. {
  175.     // If a troll walks into the trap, capture it!
  176.     if ( ptrGrid->IsTrollWithinRange( GetWorldID() ) )
  177.     {
  178.         Unveiled = true;
  179.         ptrTroll->SendCaptureMessage();
  180.         ptrGrid->Register( GetWorldID(), ENTITY_TRAP, GetX(), GetY() );
  181.     }
  182.  
  183.     // Trolls are dumb, and may forget that a "haven" is actually a 
  184.     // trap once they're gone
  185.     if( Unveiled && ptrGrid->DistanceFromTroll( GetWorldID() ) > 0 && ( rand() % 10 == 0 ) )
  186.     {
  187.         Unveiled = false;
  188.         ptrGrid->Register( GetWorldID(), ENTITY_HAVEN, GetX(), GetY() );
  189.     }
  190.  
  191.     return true;
  192. }